﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class ProtoDataToMsg : MonoBehaviour {


    public static void GetMsg(byte[] data)
    {

        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(2).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        Debug.Log(dataConcent.Length);
        Debug.Log("消息ID" + Byte2ushort(dataId));
        Pb.Hello newHello = Pb.Hello.Parser.ParseFrom(dataConcent);
        Debug.Log(newHello.Name);
    }
    //按小端，字节转为ushort型
    public static ushort Byte2ushort(byte[] byte2)
    {
        return (ushort)((byte2[0] << 8) + byte2[1]);
    }
    public static string stringToUTF8(string str)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(str);
        string msg = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
        return msg;
    }
}

